﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SWEN_HMS.Classes;
using System.Windows.Forms;

namespace SWEN_HMS.UserModule
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {

                    int id = Convert.ToInt32(Session["staffid"]);
                    staff s = userDB.getDetailByStaffID(id);
                    if (s != null)
                    {
                        lblStaffID.Text = Convert.ToString(id);
                        tbxStaffName.Text = s.staffName;
                        tbxDOB.Text = string.Format("{0:dd/MM/yyyy}", s.dob);
                        tbxEmail.Text = s.email;
                        tbxContact.Text = Convert.ToString(s.contactNo);
                        tbxAddress.Text = s.address;
                        tbxaccountNo.Text = Convert.ToString(s.accountNo);
                        ddlBank.SelectedValue = s.bankType;
                    }


                }
            }
            Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            Response.Cache.SetValidUntilExpires(false);
            Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            staff s = new staff();
            int row = 0;

            try
            {
                s.staffId = Convert.ToInt32(lblStaffID.Text);
                s.staffName = tbxStaffName.Text;
                s.dob = tbxDOB.Text;
                s.email = tbxEmail.Text;
                s.address = tbxAddress.Text;
                s.bankType = ddlBank.SelectedValue;


                if (tbxContact.Text.Length != 8)
                {
                    tbxContact.Text = "";
                    System.Windows.Forms.MessageBox.Show("Invalid Phone Number");

                }
                else
                {
                    s.contactNo = Convert.ToInt32(tbxContact.Text);
                    if (tbxaccountNo.Text.Length != 9)
                    {
                        tbxaccountNo.Text = "";
                        System.Windows.Forms.MessageBox.Show("Invalid account Number");
                    }
                    else
                    {
                        s.accountNo = Convert.ToInt32(tbxaccountNo.Text);
                        row = userDB.updateProfile(s);
                    }
                }
               

                if (row > 0)
                {
                    System.Windows.Forms.MessageBox.Show("Update Successful!");
                    Response.Redirect("~/UserModule/AccountManagement.aspx");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Update Unsuccessful!");
                }

            }
            catch (FormatException fe)
            {
                lblerrormessage.Text = " Invalid Date Format";
            }

        }

        protected void btnPW_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UserModule/AccountManagement.aspx");
        }
    }
}
